package per.chenxin.tools.foundation;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class Main {

    static public final String STR_APP_NAME = "app_name";

    static ResourceBundle resourceBundle;

    ConnectionWindow connectionWindow = null;

    ClasspathWindow classpathWindow = null;

    /**
     * Launch the application
     *
     * @param args
     */
    public static void main(String[] args) {
        Thread.currentThread().setContextClassLoader(Util.getURLClassLoader());

        String strLanguage = Util.getLanguage();
        resourceBundle = ResourceBundle.getBundle(Main.class.getName(), new Locale(strLanguage));

        try {
            Main window = new Main();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window
     */
    public void open() {

        final Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setSize(500, 375);
        Rectangle rectangleDisplay = display.getBounds();
        shell.setLocation((rectangleDisplay.width - rectangleDisplay.x - shell.getSize().x) / 2,
            (rectangleDisplay.height - rectangleDisplay.y - shell.getSize().y) / 2);
        shell.setText(resourceBundle.getString(STR_APP_NAME));
        //

        shell.open();

        final Menu menu = new Menu(shell, SWT.BAR);
        shell.setMenuBar(menu);

        final MenuItem menuItemConnect = new MenuItem(menu, SWT.NONE);
        menuItemConnect.addSelectionListener(new SelectionAdapter() {

            public void widgetSelected(final SelectionEvent e) {

                if (connectionWindow == null)
                    connectionWindow = new ConnectionWindow(shell);
                ConnectionItem connectionItem = (ConnectionItem) connectionWindow.open();
                if (connectionItem != null && Util.isConnectionItemOK(connectionItem)) {
                    DataShell dataShell;
                    try {
                        dataShell = new DataShell(shell, SWT.SHELL_TRIM, connectionItem);
                        dataShell.open();
                    } catch (ClassNotFoundException e1) {
                        Util.showErrorMessage(shell, "ClassNotFoundException:" + e1.getMessage());
                        e1.printStackTrace();
                    } catch (SQLException e1) {
                        Util.showErrorMessage(shell, "SQLException:" + e1.getMessage());
                        e1.printStackTrace();
                    } catch (InstantiationException e1) {
                        Util.showErrorMessage(shell, "InstantiationException:" + e1.getMessage());
                        e1.printStackTrace();
                    } catch (IllegalAccessException e1) {
                        Util.showErrorMessage(shell, "IllegalAccessException:" + e1.getMessage());
                        e1.printStackTrace();
                    } catch (SecurityException e1) {
                        Util.showErrorMessage(shell, "SecurityException:" + e1.getMessage());
                        e1.printStackTrace();
                    } catch (IllegalArgumentException e1) {
                        Util.showErrorMessage(shell, "IllegalArgumentException:" + e1.getMessage());
                        e1.printStackTrace();
                    } catch (NoSuchMethodException e1) {
                        Util.showErrorMessage(shell, "NoSuchMethodException:" + e1.getMessage());
                        e1.printStackTrace();
                    } catch (InvocationTargetException e1) {
                        Util.showErrorMessage(shell, "InvocationTargetException:" + e1.getMessage());
                        e1.printStackTrace();
                    }

                }

            }
        });
        menuItemConnect.setText("&Connect");

        final MenuItem menuItemOption = new MenuItem(menu, SWT.CASCADE);
        menuItemOption.setText("&Option");

        final Menu menuOption = new Menu(menuItemOption);
        menuItemOption.setMenu(menuOption);

        final MenuItem menuItemClasspath = new MenuItem(menuOption, SWT.NONE);
        menuItemClasspath.addSelectionListener(new SelectionAdapter() {

            public void widgetSelected(final SelectionEvent e) {

                if (classpathWindow == null)
                    classpathWindow = new ClasspathWindow(shell);
                classpathWindow.open();
            }
        });
        menuItemClasspath.setText("&Classpath");
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }

}
